package com.ugrow.ugrowcommunity.activity.announcement.vm;


import android.annotation.SuppressLint;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.ugrow.ugrowcommunity.R;
import com.ugrow.ugrowcommunity.activity.announcement.adapter.AnnouncementAdapter;
import com.ugrow.ugrowcommunity.activity.announcement.bean.AnnouncementBean;

import java.util.ArrayList;
import java.util.Random;


public class ActivitysFragment extends Fragment {

    private SwipeRefreshLayout swipeRefreshLayout;
    private RecyclerView recyclerView;
    private AnnouncementAdapter activitystAdapter;
    private View viewFragment;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        viewFragment = inflater.inflate(R.layout.fragment_activitys, container, false);
        initList();
        refresh();
        return viewFragment;
    }


    private void initAdapter(ArrayList<AnnouncementBean> activitysList) {
        activitystAdapter = new AnnouncementAdapter(activitysList, getActivity());
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        recyclerView = viewFragment.findViewById(R.id.rec_activitys);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(activitystAdapter);
    }

    @SuppressLint("ResourceAsColor")
    private void refresh(){
        swipeRefreshLayout = viewFragment.findViewById(R.id.swipe_refresh_layout);
        swipeRefreshLayout.setColorSchemeColors(R.color.colorPrimary);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                activitystAdapter.notifyDataSetChanged();
                swipeRefreshLayout.setRefreshing(false);
            }
        });
    }
    private void initList() {
        ArrayList<AnnouncementBean> activitys = new ArrayList<AnnouncementBean>();

        String path = "http://47.100.170.205:9000/yinggu/back_ca.png";
        for (int i = 0; i < 20; i++) {
            AnnouncementBean announcementBean = new AnnouncementBean();
            announcementBean.setTitle("学校组织学生体测");
            announcementBean.setContent(getRandomLengthContent("我校将于10月27日-30日进行学生体质测试"));
            announcementBean.setAuthor("教务处");
            announcementBean.setTime("2021年10月26日");
            announcementBean.setImgView(path);
            activitys.add(announcementBean);
        }

        initAdapter(activitys);
    }

    private String getRandomLengthContent(String content) {
        Random random = new Random();
        int length = random.nextInt(20) + 1;
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < length; i++) {
            builder.append(content);
        }
        return builder.toString();
    }

}